import utlis from '../static/js/utlis.js';
const md5  = require('./md5.js')
// import $store from '@/store/index.js'
let fristClick = 0; //处理多次点击事件,设置初始化时间

const common = {
	/*
	获取微信手机号
	*/
    getPhoneNumberPub(e) {
		let userInfo = uni.getStorageSync('userInfo')
		if (e.detail.errMsg == "getPhoneNumber:ok") {
			wx.login({
				success:function(data){
					console.log('wxcode',data.code)
					let dataObj ={
						code:data.code,
						encryptedData:e.detail.encryptedData,
						iv:e.detail.iv,
					
					}
					uni.showLoading({
						title: '加载中'
					});
					utlis.http('POST', 'mp/user/bindPhoneByWx', dataObj, function(res) {
						if(res.success){
							utlis.http('POST', 'mp/personalInfo/userInfo', {id:uni.getStorageSync('userInfo').id}, function(res) {
								userInfo.mobile = res.data.mobile;
								uni.setStorageSync('userInfo',userInfo);
								uni.$emit('refreshStorage',{msg:'更新缓存'})
								uni.hideLoading();
							})
						}else{
							uni.showToast({
								title:res.errmsg,
								icon:'none',
								duration:3500
							});
						}
						
						
						
					})
				}
			})
		} else {
		
		}
    },
    /*
	
     *带有确认和取消的弹出model框
     *@obj:{}
     */
    showModel(obj) {
        console.log(obj);
        uni.showModal({
            title: obj.title || '提示',
            content: obj.content,
            showCancel: obj.showCancel,
            cancelText: obj.cancelText || '取消',
            confirmText: obj.confirmText || '确定',
            success: function(res) {
                if (res.confirm) {
                    // $store.commit("SHOW_MODEL_NUM", 0);
                    obj.success && obj.success(obj);
                } else if (res.cancel) {
                    // $store.commit("SHOW_MODEL_NUM", 0);
                    obj.cancel && obj.cancel();
                    console.log('用户点击取消');
                }
            }
        });
    },

    /*
     *toast提示
     *@obj:{}
     */
    showToast(
        title = '',
        icon = 'none'

    ) {
        uni.showToast({
            title,
            icon,
			duration:3500
        });
    },

    /**
     * 将需要被缓存的数据持续保存在本地
     * 每次保存storage的时候需要string并做3des加密,key值为大写 
     * 所有数据增设公用集合state
     * */
    storageSet(storage_key, storage_data) {
        uni.setStorage({
            key: storage_key,
            data: JSON.stringify(storage_data)
        });
    },
    /**
     * 将被缓存的数据提取到并处理为json字符串
     * */
    storageGet(storage_key) {
        return uni.getStorageSync(storage_key) ? JSON.parse(uni.getStorageSync(storage_key)) : {};
    },

    //处理click事件点击过快产生的多次请求或者多次跳转页面,设置时间1500ms
    clickLimit(obj) {
        let nowClick = new Date().getTime();
        if (nowClick - fristClick > 1500) {
            fristClick = nowClick;
            return obj.success();
        }
    },
	
	//检测是否登录
	checkLogin(type) {
		console.log(666,type)
		if(type && type == 1){
			uni.setStorageSync('loginChoose', false);
		}
		if(!uni.getStorageSync('token')){
			console.log('loginChoose',uni.getStorageSync('loginChoose'))
			if(uni.getStorageSync('loginChoose')){
				if(uni.getStorageSync('loginChoose') == false){
					uni.setStorageSync('loginChoose', true);
					uni.navigateTo({
						url: '/pages/login/loginChoose'
					});
					return false;
				}
			}else{
				uni.setStorageSync('loginChoose', true);
				uni.navigateTo({
					url: '/pages/login/loginChoose'
				});
				return false;
			}
			// uni.navigateTo({
			// 	url: '/pages/login/loginChoose'
			// });
			// return false;
		}
		return true;
	},
	

// 接口地址：入参字段：openId  用户openidposition  5-下订详情页 6-好物商城 7-好物详情code  用于获取微信openId
	// 添加足迹
	checkTourist(position,type) {
		console.log('position',position)
        // let openId = uni.getStorageSync('userInfo').wxOpenId
        let userId = uni.getStorageSync('userInfo').id;
		let openid = uni.getStorageSync('openid') || '';
		let invitationInfo = uni.getStorageSync('invitationInfo')||{};
		
		if(openid && openid!=null && openid!='') {
			let data = {
				openId: openid,
				userId: userId,
				position: position,
				type: type||'',
				code: '',
				invitationCodeUser: invitationInfo.codeUser||'',
				invitationCodeAgency: invitationInfo.codeAgency||'',
				invitationCodeActivity: invitationInfo.codeActivity||''
			}
			console.log('存在openid',openid)
			utlis.post('mp/footprint/add', data, (res)=> {
				// console.log(res)
			})
		} else {
			console.log('不存在openid',openid)
			wx.login({
				success: function(data) {
					let params = {
						code: data.code
					}
					console.log('getUserOpenId code2:' + data.code)
					utlis.post('/mp/auth/getOpendId',params,(res)=>{
						if(res.success){
							console.log(res.data)
							uni.setStorageSync('openid', res.data.openId);
							let data = {
								openId: res.data.openId,
								userId: userId || '',
								position: position,
								type: type||'',
								code: '',
								invitationCodeUser: invitationInfo.codeUser||'',
								invitationCodeAgency: invitationInfo.codeAgency||'',
								invitationCodeActivity: invitationInfo.codeActivity||''
							}
							utlis.post('mp/footprint/add', data, (res)=> {
								// console.log(res)
							})
						}
					})
				}
			})
		}
	},
	sharedParms(options){
		console.log('options==>',options)
		let invitationInfo={
			codeUser: '',
			codeAgency: '',
			codeActivity: '',
			id:'',
			goodid: '',
			carCode: ''
		}
		if(options){
			console.log('options1==>',options)
			if(options.invitationCodeUser){
				invitationInfo.codeUser = options.invitationCodeUser || '';
			}
			if(invitationInfo.codeUser == null || invitationInfo.codeUser == ''){
				if(options.codeUser){
					invitationInfo.codeUser = options.codeUser || '';
				}
			}
			if(options.invitationCodeAgency){
				invitationInfo.codeAgency = options.invitationCodeAgency || '';
			}
			if(invitationInfo.codeAgency == null || invitationInfo.codeAgency == ''){
				if(options.codeAgency){
					invitationInfo.codeAgency = options.codeAgency || '';
				}
			}
			if(options.invitationCodeActivity){
				invitationInfo.codeActivity = options.invitationCodeActivity || '';
			}
			if(invitationInfo.codeActivity == null || invitationInfo.codeActivity == ''){
				if(options.codeActivity){
					invitationInfo.codeActivity = options.codeActivity || '';
				}
			}
			// activityCode
			if(invitationInfo.activityCode == null || invitationInfo.activityCode == ''){
				if(options.activityCode){
					invitationInfo.activityCode = options.activityCode || '';
				}
			}
			
			if(options.id){
				invitationInfo.id = options.id || '';
			}
			if(options.goodid){
				invitationInfo.goodid = options.goodid || '';
			}
			if(options.carCode){
				invitationInfo.carCode = options.carCode || '';
			}
			if (options.scene != undefined && options.scene != 'undefined') {
				let scene = decodeURIComponent(options.scene);
				let params = scene.split('&');
				console.log('scene:', params);
				console.log('params',params)
				params.forEach(item => {
					let param = item.split('=');
					if (param[0] == 'id') {
						invitationInfo.id = param[1] || '';
					}
					if (param[0] == 'carCode') {
						invitationInfo.carCode = param[1] || '';
					}
					if (param[0] == 'goodid') {
						invitationInfo.goodid = param[1] || '';
					}
					if (param[0] == 'invitationCodeUser') {
						invitationInfo.codeUser = param[1] || '';
					}
					if ((invitationInfo.codeAgency == null || invitationInfo.codeAgency == '') && param[0] == 'codeUser') {
						invitationInfo.codeUser = param[1] || '';
					}
					if (param[0] == 'invitationCodeAgency') {
						invitationInfo.codeAgency = param[1] || '';
					}
					if ((invitationInfo.codeAgency == null || invitationInfo.codeAgency == '') && param[0] == 'codeAgency') {
						invitationInfo.codeAgency = param[1] || '';
					}
					if (param[0] == 'invitationCodeActivity') {
						invitationInfo.codeActivity = param[1] || '';
					}
					if ((invitationInfo.codeActivity == null || invitationInfo.codeActivity == '') && param[0] == 'codeActivity') {
						invitationInfo.codeActivity = param[1] || '';
					}
					//activityCode
					if ((invitationInfo.activityCode == null || invitationInfo.activityCode == '') && param[0] == 'activityCode') {
						invitationInfo.activityCode = param[1] || '';
					}
				});
			}
			var  _invitationInfo = {}
			console.log('invitationInfo=>',invitationInfo)
			console.log('参数1',uni.getStorageSync('invitationInfo'))
			if(uni.getStorageSync('invitationInfo') && JSON.stringify(uni.getStorageSync('invitationInfo')) !='{}'){
				_invitationInfo = uni.getStorageSync('invitationInfo');
			}
			console.log('参数2',JSON.stringify(_invitationInfo) !='{}')
			console.log('参数3',JSON.stringify(_invitationInfo))
			if(JSON.stringify(_invitationInfo)=='{}' ){
				_invitationInfo = invitationInfo;
			}else{
				if(invitationInfo.codeUser != null && invitationInfo.codeUser != ''){
					_invitationInfo.codeUser = invitationInfo.codeUser;
				}
				if(invitationInfo.id != null && invitationInfo.id != ''){
					_invitationInfo.id = invitationInfo.id;
				}
				if(invitationInfo.goodid != null && invitationInfo.goodid != ''){
					_invitationInfo.goodid = invitationInfo.goodid;
				}
				if(invitationInfo.codeAgency != null && invitationInfo.codeAgency != ''){
					_invitationInfo.codeAgency = invitationInfo.codeAgency;
				}
				if(invitationInfo.codeActivity != null && invitationInfo.codeActivity != ''){
					_invitationInfo.codeActivity = invitationInfo.codeActivity;
				}
				// activityCode
				if(invitationInfo.activityCode != null && invitationInfo.activityCode != ''){
					_invitationInfo.activityCode = invitationInfo.activityCode;
				}
				if(invitationInfo.carCode != null && invitationInfo.carCode != ''){
					_invitationInfo.carCode = invitationInfo.carCode;
				}
			}
			uni.setStorageSync('invitationInfo', _invitationInfo);
		}
	},
	// 浏览记录 UI界面
	browsingHistory(sourceid,uid,pageurl,pagename,accesstime){
		// console.log('utlis',utlis)
		// // appkey + timestamp + secretcode
		// var appkey = 'xiaochengxu'
		// // 时间戳 截取十位
		// var timestamp = Date.parse( new Date() ).toString();
		//     timestamp = timestamp.substr(0,10);
		// //  secretcode
		// var secretcode = 'af916ba0-24aa-406c-9cc8-d951aefaa249'
		
		// var sign  = md5(appkey+timestamp+secretcode)
		// // 参数
		// let data = {
		// 	sign,
		// 	appkey,
		// 	timestamp,
		// 	sourceid,
		// 	uid,
		// 	pageurl,
		// 	pagename,
		// 	accesstime
		// }
		// if(utlis.DevOpen==true || utlis.PodOpen==true){
		//  utlis.browUrlPostUi(data, (res)=> {
		// 	console.log('访问页面记录',res)
		//  })	
		// }else{
		// 	return false
		// }
	},
	// 浏览点击记录 
	onclickHistory(sourceid,uid,actiontime,pageurl,pagename,actionid,action){
		// appkey + timestamp + secretcode
		// var appkey = 'xiaochengxu'
		// // 时间戳
		// var timestamp = Date.parse( new Date() ).toString();
		//     timestamp = timestamp.substr(0,10);
		// //  secretcode
		// var secretcode = 'af916ba0-24aa-406c-9cc8-d951aefaa249'
		
		// var sign  = md5(appkey+timestamp+secretcode)
		// // 参数
		// let data = {
		// 	sign,
		// 	appkey,
		// 	timestamp,
		// 	sourceid,
		// 	uid,
		// 	actiontime,
		// 	pageurl,
		// 	pagename,
		// 	actionid,
		// 	action
		// }
		// if(utlis.DevOpen==true || utlis.PodOpen==true){
		//  utlis.browUrlPostMed(data, (res)=> {
		// 	console.log('访问页面记录',res)
		//  })	
		// }else{
		// 	return false
		// }
	} 
};
export default common;